
package org.soekrat.gui.swing.stringmetric;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.soekrat.engine.SoekratNameMatcherSingleton;
import org.soekrat.engine.util.StringMetric;
import org.soekrat.gui.swing.TestFrame;

/**
 * <p>
 *   
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class StringMetricThresholdSlider extends JPanel 
{
  private final BoundedRangeModel m_model=new DefaultBoundedRangeModel();
  
  private final JLabel m_valueLabel=new JLabel("Match 0.00");
  
  private final JSlider m_slider=new JSlider(m_model);
  
  private StringMetric m_metric=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public StringMetricThresholdSlider()
  {
    setLayout(new BorderLayout());
    
    JPanel valuePanel=new JPanel(new FlowLayout(FlowLayout.CENTER));
    valuePanel.add(m_valueLabel);
    
    add(valuePanel, BorderLayout.NORTH);
    add(m_slider, BorderLayout.CENTER);
    
    m_slider.addChangeListener
    (
      new ChangeListener() 
      {
        public void stateChanged(ChangeEvent e)
        {
          int n=m_slider.getValue();
          float d=(float)n/(float)100.0;
          m_valueLabel.setText("Match "+Float.toString(d));
          
          if (m_metric!=null)
          {
                 if (n==0)   m_metric.setThreshold(0.0f);
            else if (n==100) m_metric.setThreshold(1.0f);
            else             m_metric.setThreshold(d);
          }
        }
      }
    );
    
    setStringMetrics(SoekratNameMatcherSingleton.INSTANCE.getStringMetric());
  }
  
  public void setStringMetrics(StringMetric metric)
  {
    float d=(metric==null) ? 0.0f : metric.getThreshold();
    m_metric=metric;
    m_slider.setValue((int)(d*100.0f));
    m_valueLabel.setText("Match "+Float.toString(d));
  }
  
  public static void main(String[] args) 
  {
    
    TestFrame.show(new StringMetricThresholdSlider());  
  }
  
}
